<template>
  <el-dropdown :max-height="290" trigger="click" placement="bottom-start">
    <el-button>
      批量操作<i style="font-size: 10px !important" class="dk-iconfont icon-ArrowDown"
    /></el-button>
    <template #dropdown>
      <template v-for="i in batchOptions" :key="i.title">
        <div class="dropdownTitle">
          {{ i.title }}
        </div>
        <template v-for="j in i.options">
          <el-dropdown-item
            class="dropdown-item"
            v-if="isPermission(j.auth)"
            @click="handleBatch(j.value)"
          >
            {{ j.label }}
          </el-dropdown-item>
        </template>
      </template>
    </template>
  </el-dropdown>
  <!-- 1 修改产品状态-->
  <SkuStatusInfo ref="skuStatusInfoRef" :options="optionsData" />
  <!-- 2  修改报关信息-->
  <CustomsInfo ref="customsInfoRef" :options="optionsData" />
  <!-- 3 修改产品分类-->
  <CertificationType ref="certificationTypeRef" :options="optionsData" />
  <!-- 4 修改SPU信息-->
  <UpdateSpuInfo ref="spuInfoRef" :options="optionsData" />
  <!-- 5 修改认证类型-->
  <ProductCategory ref="productCategoryRef" :options="optionsData" />
  <!-- 6 合并SPU-->
  <mergeSPU ref="mergeSPURef" />
  <!-- 7.修改默认供应商 -->
  <updateSupplier ref="updateSupplierRef" />
  <!-- 8.上传认证证书 -->
  <!-- 9.上产产品说明书 -->
  <!-- 10.上传专利证书 -->
  <uploadFile ref="uploadFileRef" :options="optionsData" />
  <!-- 11.修改产品标识及加工说明 -->
  <productMarking ref="productMarkingRef" :options="optionsData" />
  <!-- 12/修改人员信息 -->
  <updatePersonnel ref="updatePersonnelRef" />
  <!-- 13/禁售平台/站点 -->
  <ProhibitedPlatform ref="ProhibitedPlatformRef" />
  <!-- 14/推荐平台/站点 -->
  <RecommendationPlatform ref="RecommendationPlatformRef" />
  <!-- 15/采购天数 -->
  <updatePurchaseDay ref="updatePurchaseDayRef" :options="optionsData" />
  <!-- 16/修改重量尺寸 -->
  <updateWeightSize ref="updateWeightSizeRef" />
  <!-- 17/生成标签 -->
  <MakeLabel ref="MakeLabelRef" />
  <!-- 18/提取信息 -->
  <batchExtractInfo ref="batchExtractInfoRef" />
  <!-- 19/申报要素 -->
  <declarationElement ref="declarationElementRef" />
  <!-- 22/上传质检结果 -->
  <UploadQCResult ref="uploadQCResultRef"></UploadQCResult>
  <!-- 23/修改基础信息 -->
  <updateBasicInfo ref="updateBasicInfoRef" :options="optionsData"></updateBasicInfo>
  <!-- 24/修改产品品牌 -->
  <ProductBrand ref="ProductBrandRef" :options="optionsData"></ProductBrand>
</template>
<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { useEnumSelect } from "@/hooks/useEnumSelect.js";
import UpdateSpuInfo from "./components/UpdateSpuInfo.vue";
import CertificationType from "./components/CertificationType.vue";
import CustomsInfo from "./components/CustomsInfo.vue";
import SkuStatusInfo from "./components/SkuStatusInfo.vue";
import ProductCategory from "./components/ProductCategory.vue";
import mergeSPU from "./components/mergeSPU.vue";
import updateSupplier from "./components/updateSupplier.vue";
import uploadFile from "./components/uploadFile.vue";
import updatePersonnel from "./components/updatePersonnel.vue";
import productMarking from "./components/productMarking.vue";
import ProhibitedPlatform from "./components/ProhibitedPlatform.vue";
import RecommendationPlatform from "./components/RecommendationPlatform.vue";
import updatePurchaseDay from "./components/updatePurchaseDay.vue";
import updateWeightSize from "./components/updateWeightSize.vue";
import MakeLabel from "./components/MakeLabel.vue";
import batchExtractInfo from "@/components/Dialog/batchExtractInfo/index.vue";
import declarationElement from "./components/declarationElement";
import { batchPushToxingyun, batchImgDownload } from "./api.js";
import UploadQCResult from "./components/UploadQCResult.vue";
import updateBasicInfo from "./components/updateBasicInfo.vue";
import ProductBrand from "./components/ProductBrand.vue";

const props = defineProps({
  batchOptions: {
    type: Array,
    default: () => [],
  },
  getTable: {
    type: Function,
    default: () => {},
  },
});

const { batchOptions } = toRefs(props);

const getData = () => {
  return props.getTable();
};

const isPermission = (auth) => {
  if (!auth) return true;
  return (window.BTN_AUTH || []).includes(auth);
};

const { options: optionsData } = useEnumSelect({
  name: [
    "goods.GoodsSku",
    "goods.GoodsSkuSalesInfo",
    "goods.GoodsSpu",
    "goods.GoodsSkuCertificate",
    "goods.GoodsSkuIdentificationProcessing",
    "goods.GoodsSkuSupplier",
  ],
});

const spuInfoRef = ref();
const certificationTypeRef = ref();
const customsInfoRef = ref();
const productCategoryRef = ref();
const skuStatusInfoRef = ref();
const mergeSPURef = ref();
const updateSupplierRef = ref();
const uploadFileRef = ref();
const updatePersonnelRef = ref();
const productMarkingRef = ref();
const ProhibitedPlatformRef = ref();
const RecommendationPlatformRef = ref();
const updatePurchaseDayRef = ref();
const updateWeightSizeRef = ref();
const MakeLabelRef = ref();
const batchExtractInfoRef = ref();
const declarationElementRef = ref();
const uploadQCResultRef = ref();
const updateBasicInfoRef = ref();
const ProductBrandRef = ref();
const emits = defineEmits(["batch"]);
const handleBatch = (name) => {
  const data = getData();
  if (!data?.records?.length) {
    ElMessage.warning("请先选择行");
    return;
  }

  if ((name == 2 || name == 7) && data?.records?.length > 100) {
    if (name == 2) {
      ElMessage.warning("批量修改报关信息，最多支持勾选100个SKU修改，请知悉");
    } else if (name == 7) {
      ElMessage.warning("批量修改默认供应商及价格信息，最多支持勾选100个SKU修改，请知悉");
    }
    return;
  }

  const spu_ids = data?.records?.map((i) => i.spu_id);
  const sku_ids = data?.records?.map((i) => i.id);

  const dev_status = data?.records?.some((i) => i.dev_status >= 4);
  const records = {
    ids: data.name === "spu_ids" ? spu_ids : sku_ids,
    name: data.name,
    dev_status,
    spu_category: data?.records?.map((i) => {
      return { spu_category: i.spu_category };
    }),
    is_refund_tax: data?.records?.map((i) => i.goods_sku_supplier?.is_refund_tax),
    sku_inventory_list: data?.records?.map((i) => {
      return {
        sku: i.sku,
        inventory: i.inventory,
      };
    }),
    combined_sources: data?.records?.map((i) => {
      return { combined_sources: Number(i.combined_sources) };
    }),
  };

  const weightSizeRecord = data?.records?.map((i) => {
    return {
      sku_ids: i.id,
      spu_category: i.spu_category,
    };
  });

  const spu_records = data.records.filter((i) => i.hasChild);

  const open = {
    1: () => {
      skuStatusInfoRef.value.open(records);
    },
    2: () => {
      customsInfoRef.value.open(records);
    },
    3: () => {
      productCategoryRef.value.open(spu_ids);
    },
    4: () => {
      spuInfoRef.value.open(spu_ids);
    },
    5: () => {
      certificationTypeRef.value.open(records);
    },
    6: () => {
      if (data?.records?.length < 2) {
        ElMessage({
          showClose: true,
          message: "请至少选择两条数据！",
          type: "warning",
        });
        return;
      }
      mergeSPURef.value.open(spu_records);
    },
    7: () => {
      updateSupplierRef.value.open(records);
    },
    8: () => {
      uploadFileRef.value.open({ type: 1, data: records });
    },
    9: () => {
      uploadFileRef.value.open({ type: 2, data: records });
    },
    10: () => {
      uploadFileRef.value.open({ type: 3, data: records });
    },
    12: () => {
      updatePersonnelRef.value.open(sku_ids);
    },
    11: () => {
      productMarkingRef.value.open(records);
    },
    13: () => {
      ProhibitedPlatformRef.value.open(records);
    },
    14: () => {
      RecommendationPlatformRef.value.open(records);
    },
    15: () => {
      updatePurchaseDayRef.value.open(sku_ids);
    },
    16: () => {
      updateWeightSizeRef.value.open(weightSizeRecord);
    },
    17: () => {
      MakeLabelRef.value.open(sku_ids);
    },
    18: () => {
      batchExtractInfoRef.value.open(data?.records);
    },
    19: () => {
      declarationElementRef.value.open(records);
    },
    20: () => {
      batchPushxingyun(sku_ids);
    },
    21: () => {
      if (sku_ids?.length > 10) {
        ElMessage({
          message: "下载图片内容较大，请勾选十个及以下SKU进行下载",
          type: "error",
        });
        return;
      }
      batchImageDownload(sku_ids);
    },
    22: () => {
      uploadQCResultRef.value.open(sku_ids);
    },
    23: () => {
      updateBasicInfoRef.value.open(sku_ids);
    },
    24: () => {
      ProductBrandRef.value.open(sku_ids);
    },
  };
  open[name]();
};

// 推送到兴运
const batchPushxingyun = async (sku_ids) => {
  try {
    const res = await batchPushToxingyun({ sku_ids });
    ElMessage.success(res.message);
  } catch (e) {
    throw Error(e);
  }
};

// 批量图片下载
const batchImageDownload = async (sku_ids) => {
  try {
    const res = await batchImgDownload({ sku_ids });
    ElMessage.success(
      "发起图片下载成功，请稍后前往基础管理-图片下载任务中进行图片包下载"
    );
  } catch (e) {
    throw Error(e);
  }
};
</script>
<style lang="scss">
.dropdownTitle {
  // font-size: 12px;
  // padding: 6px 16px;
  // color: var(--base-sub-text-color);
  font-size: 12px;
  padding: 8px 16px 0 0;
  color: #9da1ad;
  margin-left: 8px;

  &:not(:first-child) {
    border-top: 1px solid #e6e7e9;
  }
}

.dropdown-item {
  color: var(--base-text-color);
  // font-size: 16px;
}
</style>
